package com.trinity.specialpay.controller;

import com.trinity.campus.domain.CampusStudents;
import com.trinity.campus.service.ICampusStudentsService;
import com.trinity.common.annotation.Log;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.page.TableDataInfo;
import com.trinity.common.enums.BusinessType;
import com.trinity.common.utils.poi.ExcelUtil;
import com.trinity.specialpay.domain.CampusActivity;
import com.trinity.specialpay.domain.CampusActivityStudent;
import com.trinity.specialpay.dto.QueryActivityParamsDto;
import com.trinity.specialpay.dto.QueryActivityStudentsResultDto;
import com.trinity.specialpay.dto.UpdateActivityStudentsDto;
import com.trinity.specialpay.service.ICampusActivityService;
import com.trinity.specialpay.service.ICampusActivityStudentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 缴费名单Controller
 *
 * @author trinity
 * @date 2023-11-21
 */
@RestController
@RequestMapping("/campus/act_stu")
public class CampusActivityStudentController extends BaseController {
    @Resource
    private ICampusActivityStudentService campusActivityStudentService;
    @Resource
    private ICampusActivityService campusActivityService;

    @Resource
    private ICampusStudentsService campusStudentsService;

    /**
     * 查询缴费名单列表
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueryActivityParamsDto queryActivityParamsDto) {
        startPage();
        List<CampusActivityStudent> list = campusActivityStudentService.selectCampusActivityStudentList(queryActivityParamsDto);
        return getDataTable(list);
    }

    /**
     * 导出缴费名单列表
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:export')")
    @Log(title = "缴费名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QueryActivityParamsDto queryActivityParamsDto) {
        List<CampusActivityStudent> list = campusActivityStudentService.selectCampusActivityStudentList(queryActivityParamsDto);
        ExcelUtil<CampusActivityStudent> util = new ExcelUtil<CampusActivityStudent>(CampusActivityStudent.class);
        util.exportExcel(response, list, "缴费名单数据");
    }

    /**
     * 获取缴费名单详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:query')")
    @GetMapping(value = "/{payId}")
    public AjaxResult getInfo(@PathVariable("payId") Long payId) {
        return success(campusActivityStudentService.selectCampusActivityStudentByPayId(payId));
    }

    /**
     * 获取缴费名单详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:query')")
    @GetMapping(value = "/stu/{actId}")
    public AjaxResult getActStudents(@PathVariable("actId") Long actId) {
        QueryActivityStudentsResultDto queryActivityStudentsResultDto = new QueryActivityStudentsResultDto();
        //活动项目
        CampusActivity campusActivity = campusActivityService.selectCampusActivityByActId(actId);
        if (campusActivity == null) {
            return error("缴费项目不存在！");
        }
        queryActivityStudentsResultDto.setActivity(campusActivity);
        //该班所有学生
        CampusStudents campusStudent = new CampusStudents();
        campusStudent.setDeptId(String.valueOf(campusActivity.getDeptId()));
        List<CampusStudents> campusStudents = campusStudentsService.selectCampusStudentsList(campusStudent);
        queryActivityStudentsResultDto.setDeptStudents(campusStudents);
        //参与名单
        QueryActivityParamsDto queryActivityParamsDto = new QueryActivityParamsDto();
        queryActivityParamsDto.setActId(actId);
        List<CampusActivityStudent> campusActivityStudents = campusActivityStudentService.selectCampusActivityStudentList(queryActivityParamsDto);
        for (CampusActivityStudent stu : campusActivityStudents) {
            queryActivityStudentsResultDto.addStudent(Long.valueOf(stu.getStudentId()));
        }
        return success(queryActivityStudentsResultDto);
    }

    /**
     * 新增缴费名单
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:add')")
    @Log(title = "缴费名单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CampusActivityStudent campusActivityStudent) {
        return toAjax(campusActivityStudentService.insertCampusActivityStudent(campusActivityStudent));
    }

    /**
     * 批量增加缴费名单
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:add')")
    @Log(title = "缴费名单", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public AjaxResult add(@RequestBody UpdateActivityStudentsDto activityStudentsDto) {
        String students = activityStudentsDto.getStudents();
        Long[] resultList = Arrays.stream(students.split(","))
                .map(Long::valueOf)
                .toArray(Long[]::new);
        return toAjax(campusActivityStudentService.batchActStudent(activityStudentsDto.getActId(), resultList));
    }

    /**
     * 修改缴费名单
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:edit')")
    @Log(title = "缴费名单", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody CampusActivityStudent campusActivityStudent) {
        return toAjax(campusActivityStudentService.updateCampusActivityStudent(campusActivityStudent));
    }

    /**
     * 删除缴费名单
     */
    @PreAuthorize("@ss.hasPermi('campus:act_stu:remove')")
    @Log(title = "缴费名单", businessType = BusinessType.DELETE)
    @PostMapping("/{payIds}/delete")
    public AjaxResult remove(@PathVariable Long[] payIds) {
        return toAjax(campusActivityStudentService.deleteCampusActivityStudentByPayIds(payIds));
    }
}
